package com.ruanyuan.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruanyuan.pojo.MessInfo;
import com.ruanyuan.pojo.Reply;
import com.ruanyuan.service.MessInfoService;
import com.ruanyuan.service.ReplyService;
import com.ruanyuan.utils.Page;

/**
 * 留言信息控制器
 * 
 * @author zbp
 *
 */
@Controller
@RequestMapping("/Message")
public class MessageController {
	// 注入留言信息业务类
	@Autowired
	private MessInfoService messInfoService;
	// 注入回复业务层
	@Autowired
	private ReplyService replyService;

	// 展示所有留言信息
	@RequestMapping("/allMess")
	public String showAllMessage(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "4") Integer rows, Model model) {
		// 执行查询所有留言信息方法
		Page<MessInfo> messInfos = messInfoService.getAllMessInfos(page, rows);
		List<MessInfo> result = messInfos.getResult();
		for (MessInfo messInfo : result) {
			// 获取留言回复量
			int replyNum = replyService.getReplyNum(messInfo.getMessId());
			// 根据时间查询最新回复信息
			Reply byTime = replyService.getReplyByTime(messInfo.getMessId());
			if (byTime != null) {
				messInfo.setAuthor(byTime.getReplyAuthor());
				messInfo.setReplyNum(replyNum);
				// 实例化回复实体类
				Reply reply = new Reply();
				reply.setReplyTime(byTime.getReplyTime());
				reply.setReplyAuthor(byTime.getReplyAuthor());
				messInfo.setReply(reply);
			}
		}
		// 将留言信息集合传给视图
		model.addAttribute("page", messInfos);
		return "index";
	}

	// 跳转至标题展示页
	@RequestMapping("/showTitleInfo/{id}")
	public String toTitleInfo(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "4") Integer rows, @PathVariable("id") Integer id, Model model,
			HttpSession session) {
		// 调用查询所有评论信息方法
		Page<Reply> allReplys = replyService.getAllReply(page, rows, id);
		// 实例化留言对象
		MessInfo messInfoById = messInfoService.getMessInfoById(id);
		// 点击量+1
		messInfoService.updateClickNum(id);
		// 将查询结果传递至视图
		model.addAttribute("page", allReplys);
		model.addAttribute("id", id);
		// 将留言信息传递为视图
		model.addAttribute("messById", messInfoById);
		// 跳转至留言界面
		return "hello";
	}

	// 跳转至发布新留言页面
	@RequestMapping("/toNewMess")
	public String toaddNewMess() {
		return "newMess";
	}

	// 发布新留言
	@RequestMapping("/addNewMess")
	@ResponseBody
	public int addNewMess(String titleName, String titleInfo, String author, HttpSession session) {
		// 获取session内容
		String userName = (String) session.getAttribute("userName");
		// 实例化留言信息
		MessInfo mInfo = new MessInfo();
		mInfo.setAuthor(userName);
		mInfo.setTitleName(titleName);
		mInfo.setTitleInfo(titleInfo);
		// 调用添加留言方法
		int rows = messInfoService.addMessInfo(mInfo);
		if (rows > 0) {
			System.out.println("成功");
			return 1;
		} else {
			return 0;
		}
	}

}
